//
//  KSNotDataTool.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/4/6.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit
class KSNotDataTool: NSObject {
    
}
protocol KSNotData {
    var showView:UIView? {get}
}

enum KSNotDataType {
    case normal
case notData
    case search
    func getImgAndRect() -> (String,CGSize) {
        var imgName = ""
        
        var size = CGSize()
        
        switch self{
            case.normal:
                imgName = "无数据01"
                size = CGSize(width:148.0 / 2 * KSGlobal.scale375, height:171.0 / 2 * KSGlobal.scale375)
            case.notData:
                imgName = "zanwushuju-3"
                size = CGSize(width:300.0 / 2 * KSGlobal.scale375, height:256.0 / 2 * KSGlobal.scale375)
            case.search:
                imgName = "icon__saearch"
                size = CGSize(width:189.0 / 2 * KSGlobal.scale375, height:185.0 / 2 * KSGlobal.scale375)
        }
        return(imgName,size)
    }
    
}


extension KSNotData {
    
    ///LsqNotDataType 显示的类型，说白了就是选择哪个图片
    
    ///point为显示的中心点，如果为空，则默认为 showView 的中心点
    
    ///text在图片下放的文字
    
    ///offset 图片的偏移位置，为nil则不偏移。x,y的值如果<0,则向左和上偏移
    
    func showNotData(with type: KSNotDataType, text: String?,topOffset:CGFloat = 0) {
        self.removeNotDataView()
        
        if self.showView == nil {
            return
        }
        
        let(imgName,size) = type.getImgAndRect()
        
        var frame = CGRect()
        frame = CGRect(x: (KSGlobal.screenW - size.width)/2, y: (KSGlobal.screenH-size.height)/2 + topOffset, width: size.width, height: size.height)
        
        
        let imgView = UIImageView(frame: frame)
        
        imgView.image = UIImage(named: imgName)
        
        imgView.tag = 6667771
        
        self.showView?.addSubview(imgView)
        
        let label = UILabel(frame:CGRect(x:0, y: imgView.ks_maxY + 10, width:self.showView!.frame.width, height:20 * KSGlobal.scale375))
        
        label.text = text
        
        label.numberOfLines = 0
        
        label.font = UIFont.systemFont(ofSize: 15)
        
        label.textAlignment = .center
        
        label.textColor = UIColor(kshex: "5e7181")
        
        label.tag = 6667772
        
        label.sizeToFit()
        
        label.center.x = self.showView!.frame.width/2
        
        self.showView?.addSubview(label)
        
    }
    
    ///调用该方法移除无数据视图
    
    func removeNotDataView(){
        
        let imgView1 = self.showView?.viewWithTag(6667771)
        
        imgView1?.removeFromSuperview()
        
        let label1 = self.showView?.viewWithTag(6667772)
        
        label1?.removeFromSuperview()
        
        let other = self.showView?.viewWithTag(6667773)
        
        other?.removeFromSuperview()
        
    }
    
    ///添加一个其他视图，可以点击的
    
    func addOtherNotDataView(_ view:UIView?, labelBottomY:CGFloat, target:Any?, action:Selector?) {
        
        guard let vv = view else { return }
        
        if let act = action {
            
            view?.isUserInteractionEnabled = true
            
            let tap = UITapGestureRecognizer(target: target, action: act)
            
            vv.addGestureRecognizer(tap)
            
        }
        
        if let label = self.showView?.viewWithTag(6667772) {
            
            vv.ks_y = label.ks_bottom + labelBottomY
            
            vv.center.x = label.center.x
            
            vv.tag=6667773
            
            self.showView?.addSubview(vv)
            
        }
        
    }
    
}

//class MessgeTableView: UITableView,LsqNotData {
//
//    var showView:UIView? {
//
//        return self
//
//    }
//
//    public var dadaArry = [Model](){
//
//        didSet{
//
//            if self.dadaArry.isEmpty{
//
//                self.showNotData(with: .notData, point:nil, text:"暂无数据", offset:nil)
//
//            }else{
//
//                self.removeNotDataView()
//
//            }
//
//            self.reloadData()
//
//        }
//
//    }
//
//}
//class AddressBookSearchController: BaseViewController, LsqNotData {
//
//    public var dataArray = [AddressBookModel]() {
//
//        didSet{
//
//            if self.dataArray.isEmpty{
//
//                self.showNotData(with: .notData, point:nil, text:"暂无数据", offset:nil)
//
//            }else{
//
//                self.removeNotDataView()
//
//            }
//
//            self.myTabView?.reloadData()
//
//        }
//
//    }
//
//    private var myTabView:UITableView?
//
//    var showView:UIView? {
//
//        return self.myTabView
//
//    }
//
//}
//
